﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SpeechLib;
using System.Threading;
using System.Diagnostics;
using System.Speech.Synthesis;
using System.Globalization;

namespace Project_V1
{

   
    public partial class FrmSysDebug : Form
    {
        DataTable dt = new DataTable("送钉测试");
        DataRow dr;
        Thread t1;
        public string FileName = "送钉测试统计表.csv";
        public int ID = 0;//流水号
        //public int ID = 0;//流水号 
        public int FeedMachLastCycleCounter = 0 , FeedMachLastCycleCounterCnt = 0;//送钉机上次循环序号
        public int FeedMachNowCycleCounter = 0;//送钉机当前循环序号
        public long FeedMachRivetOutCounter = 0;//送钉机出钉序号
        public long CachePlaceRivetInCounter = 0;//缓存处进钉序号
        public long FeedMachRivetOutOverTimeReset = 0, FeedMachRivetOutOverTimeReset_bak = 0, FeedMachRivetOutOverTimeResetCnt = 0;//送钉机出钉是否超时报警
        public long CachePlaceRivetInOverTimeReset = 0, CachePlaceRivetInOverTimeReset_bak = 0, CachePlaceRivetInOverTimeResetCnt = 0;//缓存处进钉是否超时报警
        public long CachePlaceRivetAtOverTimeReset = 0, CachePlaceRivetAtOverTimeReset_bak = 0, CachePlaceRivetAtOverTimeResetCnt = 0;//缓存处铆钉到位是否超时报警
        public string FeedMachRivetOutOverTimeResetStr = "";//送钉机出钉是否超时报警
        public string CachePlaceRivetInOverTimeResetStr = "";//缓存处进钉是否超时报警
        public string CachePlaceRivetAtOverTimeResetStr = "";//缓存处铆钉到位是否超时报警
        public string FeedMachOK = "";                  //送钉机送钉是否成功
        public string FeedMachAlermType = "";        //送钉机送钉失败报警类别
        public DateTime MachCachePlaceStartTime = System.DateTime.Now;//铆钉从振动盘到缓存处开始日期
        public DateTime MachCachePlaceEndTime = System.DateTime.Now;//铆钉从振动盘到缓存处结束日期
        public string MachCachePlaceDealTime = "";//铆钉从振动盘到缓存处用时
        public int GunLastCycleCounter = 0, GunLastCycleCounter_bak = 0, GunLastCycleCounterCnt = 0;//铆枪上次循环序号
        public int GunNowCycleCounter = 0, GunNowCycleCounter_bak = 0;//铆枪当前循环序号
        public long RivetInCounter = 0;//缓存处出钉序号
        public long RivetOutCounter = 0;//枪头处进钉序号
        public long InhaleRivetOKCounter = 0;//吸钉成功计数
        public long InhaleRivetNOKCounter = 0;//吸钉失败序号
        public long InhaleRivetActualCounter = 0;//当前序号铆钉实际吸钉次数
        public long TestFeedingTime = 0;//当前序号铆钉送吸钉成功用时

        public string EnableVacuoValve_VacuoRchTime = "";//吸钉阀打开后到真空达到用时       
        public string SingleCycleVacuoValue = "0";//真空达到
        public string EnableVacuoValveLater_VacuoValue1 = "0";//吸钉阀打开
        public string EnableVacuoValveLater_VacuoValue2 = "0";//吸钉阀打开

        public string EnableVacuoValve_VacuoRchTimeStr = "";
        public string SingleCycleVacuoValueStr = "";
        public string EnableVacuoValveLater_VacuoValue1Str = "";
        public string EnableVacuoValveLater_VacuoValue2Str = "";
         
        public long CachePlaceRivetOutOverTimeReset = 0, CachePlaceRivetOutOverTimeReset_bak = 0, CachePlaceRivetOutOverTimeResetCnt = 0;//缓存处出钉是否超时报警
        public long GunHeadRivetInOverTimeReset = 0, GunHeadRivetInOverTimeReset_bak = 0, GunHeadRivetInOverTimeResetCnt = 0;//枪头处进钉是否超时报警
        public long FeedingNOKLater_CycleContinue = 0, Per_InhaleRivetOverTimeReset_bak = 0, Per_InhaleRivetOverTimeResetCnt = 0;//当前序号铆钉吸钉是否失败报警
        public long InhaleRivetLater_DisableVacuoReset = 0, InhaleRivetLater_DisableVacuoReset_bak = 0, InhaleRivetLater_DisableVacuoResetCnt = 0;//吸钉成功后移动是否掉真空报警

        public long CachePlaceBlowRivetOverTimeReset = 0, CachePlaceBlowRivetOverTimeReset_bak = 0, CachePlaceBlowRivetOverTimeResetCnt = 0;//缓存处出钉是否超时报警 
        public string VacuoRchLater_SampleTime = "", EnableVacuoValveLater_SampleTime1 = "", EnableVacuoValveLater_SampleTime2 = ""; 
        public long FeedingNOKAlarm = 0, FeedingNOKAlarm_bak = 0, FeedingNOKAlarmCnt = 0; 
        public long InhaleRivetLater_DisableVacuoAlarm_bak = 0, InhaleRivetLater_DisableVacuoAlarmCnt = 0;
        public string CachePlaceRivetOutOverTimeResetStr = "";//缓存处出钉是否超时报警
        public string FeedAlarmStr = "", FeedAlarmStr_bak = "",  GunMachAlermstr = "" ,GunMachAlermstr_bak= "";

        public string GunHeadRivetInOverTimeResetStr = "";//枪头处进钉是否超时报警 
        public string InhaleRivetLater_DisableVacuoResetStr = "";//吸钉成功后移动是否掉真空报警
        public string FeedingNOKAlarmStr = "";
    
        public string CachePlaceBlowRivetOverTimeResetStr = ""; 
        public string GunMachOK = "";                  //送钉机送钉是否成功
        public string GunMachAlermType = "";        //送钉机送钉失败报警类别
        public DateTime GunCachePlaceStartTime = new DateTime();//铆钉从振动盘到缓存处开始日期
        public DateTime GunCachePlaceEndTime = new DateTime();//铆钉从振动盘到缓存处结束日期
        public string GunCachePlaceDealTime = "";//铆钉从振动盘到缓存处用时       
        public long FeedMachNowCycleing = 0 , FeedMachNowCycleingCnt = 0;//送钉机当前循环进行中, FeedMachNowCycleing_bak = 0
        public string FeedMachNowCycleing_bak = "False";
        public long GunNowCycleing = 0 , GunNowCycleingCnt = 0;  //铆枪当前循环进行中
        public string GunNowCycleing_bak = "False";
        public string FeedMachNowCycleingStr = "";
        public string GunNowCycleingStr = "";
        public string FeedMachRivetOutOverTimeAlarm = "0";
        public string CachePlaceRivetInOverTimeAlarm = "0";
        public string CachePlaceRivetAtOverTimeAlarm = "0";
        public string FeedMachBlowRivetOverTimeAlarm = "0";      
        public string CachePlaceRivetOutOverTimeAlarm = "0";
        public string GunHeadRivetInOverTimeAlarm = "0";
        public string CachePlaceBlowRivetOverTimeAlarm = "0";
        public string FeedingNOK = "0";
        public string InhaleRivetLater_DisableVacuoAlarm = "0";
        public string btFeedingTestStart_Stop = "0" , btFeedingTestStart_Stopbak = "0";
        public string AlarmStr = "", AlarmStrBak = "", AlarmSpeakStrBak = "" ;
        public int AlarmCnt = 0;
        public int FeedMachNowCycleCounter_Start = 0 , GunNowCycleCounter_Start = 0;
        int FeedMachNowCycleing_UP_cnt = 0, GunNowCycleCounter_UP_cnt = 0;



        delegate void updatedgviewDelegate(DataTable dt);
        private void SetdgviewFunction(DataTable dt)
        {
            if (dgv_data.InvokeRequired)
            {
                updatedgviewDelegate delegateSetSource = new updatedgviewDelegate(SetdgviewFunction);

                dgv_data.Invoke(delegateSetSource, new object[] { dt });
            }
            else
            {
                dgv_data.DataSource = dt;
            }
        }


        public int testguncnt = 0;
        public FrmSysDebug()
        {
            InitializeComponent();
            this.timer1.Interval = 100;
            this.Load += FrmSysDebug_Load;
        }

        private void FrmSysDebug_Load(object sender, EventArgs e)
        {
            this.timer1.Enabled = true;
        }

        //双击事件,将值传给参数修改界面,弹出界面
        private void ModifyTxt_UserControlClick(object sender, EventArgs e)
        {
            if (sender is TextBoxControl)
            {
                TextBoxControl item = (TextBoxControl)sender;

                string InitialValue = item.ActualValue;//将控件读取的ActualValue赋值给参数修改界面的Label显示

                string Variable = string.Empty;
                if (item.Tag != null)//由控件Tag的值得到变量名
                {
                    Variable = "_03_HMI." + item.Tag.ToString();//拼接变量名
                }
                FrmParaModify objFrm = new FrmParaModify(InitialValue, Variable);
                objFrm.ShowDialog();
            }
        }

        private void ModifyTxt_Click(object sender, EventArgs e)
        {
            if (sender is TextBox)
            {
                TextBox item = (TextBox)sender;

                string InitialValue = item.Text;//将控件读取的ActualValue赋值给参数修改界面的Label显示

                string Variable = string.Empty;
                if (item.Tag != null)//由控件Tag的值得到变量名
                {
                    Variable = "_03_HMI." +item.Tag.ToString();//拼接变量名 "." +
                }
                FrmParaModify objFrm = new FrmParaModify(InitialValue, Variable);
                objFrm.ShowDialog();
            }
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
        
            Beckhoff_PLC.IsWriting = true;
            if (CommonMethods.CurrentValue["_03_HMI.btFeedingTestStart_Stop"] == "False")
            {
                FeedMachNowCycleing_bak = "False";
                GunNowCycleing_bak = "False";
                Beckhoff_PLC.WriteVariable("_03_HMI.btFeedingTestStart_Stop", true);
            }
            if (CommonMethods.CurrentValue["_03_HMI.btFeedingTestStart_Stop"] == "True")
            {
                Beckhoff_PLC.WriteVariable("_03_HMI.btFeedingTestStart_Stop", false);
            }
            Beckhoff_PLC.IsWriting = false;
        }
         
        public void Init()
        { 
            MachCachePlaceDealTime = "";
            GunCachePlaceDealTime = "";
       
            FeedMachLastCycleCounterCnt = 0;
            GunLastCycleCounterCnt = 0;
            FeedMachRivetOutOverTimeResetCnt = 0;
            CachePlaceRivetInOverTimeResetCnt = 0;
            CachePlaceRivetAtOverTimeResetCnt = 0;

            CachePlaceRivetOutOverTimeResetCnt = 0;
            GunHeadRivetInOverTimeResetCnt = 0;
            Per_InhaleRivetOverTimeResetCnt = 0;
            InhaleRivetLater_DisableVacuoResetCnt = 0;
             
            FeedMachRivetOutOverTimeReset = 0; FeedMachRivetOutOverTimeReset_bak = 0; FeedMachRivetOutOverTimeResetCnt = 0; 
            CachePlaceRivetInOverTimeReset = 0; CachePlaceRivetInOverTimeReset_bak = 0; CachePlaceRivetInOverTimeResetCnt = 0; 
            CachePlaceRivetAtOverTimeReset = 0; CachePlaceRivetAtOverTimeReset_bak = 0; CachePlaceRivetAtOverTimeResetCnt = 0; 
            //FeedMachRivetOutOverTimeResetStr = "";
            //CachePlaceRivetInOverTimeResetStr = "";
            //CachePlaceRivetAtOverTimeResetStr = "";
             
            CachePlaceRivetOutOverTimeReset = 0;CachePlaceRivetOutOverTimeReset_bak = 0;CachePlaceRivetOutOverTimeResetCnt = 0; 
            GunHeadRivetInOverTimeReset = 0;GunHeadRivetInOverTimeReset_bak = 0;GunHeadRivetInOverTimeResetCnt = 0; 
            FeedingNOKLater_CycleContinue = 0;Per_InhaleRivetOverTimeReset_bak = 0;Per_InhaleRivetOverTimeResetCnt = 0; 
            InhaleRivetLater_DisableVacuoReset = 0;InhaleRivetLater_DisableVacuoReset_bak = 0;InhaleRivetLater_DisableVacuoResetCnt = 0;
        
            FeedMachNowCycleing = 0 ;FeedMachNowCycleingCnt = 0;
            GunNowCycleing = 0 ; GunNowCycleingCnt = 0; GunLastCycleCounter_bak = 0 ;
            CachePlaceBlowRivetOverTimeReset = 0; CachePlaceBlowRivetOverTimeReset_bak = 0; CachePlaceBlowRivetOverTimeResetCnt = 0;//缓存处出钉是否超时报警
            FeedingNOKAlarm = 0; FeedingNOKAlarm_bak = 0; FeedingNOKAlarmCnt = 0;
            FeedingNOKAlarmStr = "";
            FeedMachNowCycleingStr = "";
            GunNowCycleingStr = "";
            VacuoRchLater_SampleTime = "";
            EnableVacuoValveLater_SampleTime1 = "";
            EnableVacuoValveLater_SampleTime2 = ""; 
            FeedingNOK = "0";
            CachePlaceBlowRivetOverTimeAlarm = "0";
 
            SingleCycleVacuoValue = "0";//真空达到
            EnableVacuoValveLater_VacuoValue1 = "0";//吸钉阀打开
            EnableVacuoValveLater_VacuoValue2 = "0";//吸钉阀打开

  

 


            CachePlaceBlowRivetOverTimeResetStr = "";

            FeedMachRivetOutOverTimeResetCnt = 0;
            CachePlaceRivetInOverTimeResetCnt = 0;
            CachePlaceRivetAtOverTimeResetCnt = 0;

            CachePlaceRivetOutOverTimeResetCnt = 0;
            GunHeadRivetInOverTimeResetCnt = 0;
            CachePlaceBlowRivetOverTimeResetCnt = 0;
            FeedingNOKAlarmCnt = 0;
            InhaleRivetLater_DisableVacuoAlarmCnt = 0;

            MachCachePlaceEndTime = System.DateTime.Now;
            MachCachePlaceStartTime = System.DateTime.Now;

            GunCachePlaceStartTime = System.DateTime.Now;
            GunCachePlaceEndTime = System.DateTime.Now;
 
        }

        public void GetV(string t,string v)
        { 
            try
            {
                //t = t.Replace("_03_HMI.", "");
                if (v == "" || v == null) return;
                if (v == "False") v = "0";
                else if (v == "True") v = "1";
                switch (t)
                {                          
                    case "FeedMachLastCycleCounter":  if (FeedMachLastCycleCounterCnt == 2) {
                           
                            FeedMachLastCycleCounter = Convert.ToInt16(v);
                          }; 
                        break;
                    case "FeedMachNowCycleCounter":
                            if (Convert.ToInt16(v) != FeedMachNowCycleCounter && Convert.ToInt16(v) != 0)
                            {
                                FeedMachNowCycleCounter = Convert.ToInt16(v); 
                                FeedMachNowCycleCounter_Start++;
                                InsertNewDT();  //插如新行 

                                FeedMachAlermType = "";
                                FeedMachRivetOutOverTimeResetStr = "";
                                CachePlaceRivetInOverTimeResetStr = "";
                                CachePlaceRivetAtOverTimeResetStr = "";
                                FeedMachNowCycleing_UP_cnt = 0;

                                 MachCachePlaceStartTime = System.DateTime.Now;
                                dt.Rows[FeedMachNowCycleCounter_Start - 1]["送钉机上次循环序号"] = (Convert.ToInt16(CommonMethods.CurrentValue["_03_HMI.FeedMachNowCycleCounter"]) - 1).ToString();
                                dt.Rows[FeedMachNowCycleCounter_Start - 1]["送钉机当前循环序号"] = CommonMethods.CurrentValue["_03_HMI.FeedMachNowCycleCounter"];
                                dt.Rows[FeedMachNowCycleCounter_Start - 1]["铆钉从振动盘到缓存处开始日期"] = MachCachePlaceStartTime.ToString("yyyy-MM-dd_HH:mm:ss");
 
                        }
                        break;
                    case "FeedMachRivetOutCounter": FeedMachRivetOutCounter = Convert.ToInt32(v); break;
                    case "CachePlaceRivetInCounter": CachePlaceRivetInCounter = Convert.ToInt32(v); break;
                    case "btFeedingTestStart_Stop":
                        {   
                            if (v != btFeedingTestStart_Stop )
                            { 
                                if(v == "1")  {
                                    testguncnt++; 
                                    Init();
                                    InitDT();//开始运行  
                                    FeedMachNowCycleCounter_Start = 0;
                                    GunNowCycleCounter_Start = 0; 
                                    GunNowCycleCounter = 0;
                                    FeedMachNowCycleCounter = 0;
                                }
                                else if(v == "0")      {  //点结束后生成EXECL文件
                                    FileName = "送钉测试统计表_" + System.DateTime.Now.ToString("yyyyMMddHHmm") + ".csv";
                                    UpdateDT();
                                    Toexcel.DataTableToCSV(dt, System.Environment.CurrentDirectory + "\\Log\\" + FileName);
                                }
                                btFeedingTestStart_Stop = v;
                            }
                        } break; 
           

                    case "FeedMachAlermType":
                        if (FeedMachRivetOutOverTimeResetStr != "")
                        {
                            FeedMachAlermType = "送钉机出钉是否超时报警" + " ";
                        }
                        if (CachePlaceRivetInOverTimeResetStr != "")
                        {
                            FeedMachAlermType += "缓存处进钉是否超时报警" + " ";
                        }
                        if (CachePlaceRivetAtOverTimeResetStr != "")
                        {
                            FeedMachAlermType += "缓存处铆钉到位是否超时报警" + " ";
                        }
                        break;
                   
                    case "GunLastCycleCounter":  //GunNowCycleCounter_Start
                        if (GunLastCycleCounterCnt == 2) { GunLastCycleCounter = Convert.ToInt16(v);  }; //testguncnt++;
                        dt.Rows[GunNowCycleCounter_Start - 1]["铆枪上次循环序号"] = (Convert.ToInt16( CommonMethods.CurrentValue["_03_HMI.GunNowCycleCounter"]  ) - 1).ToString();
                        break;
                        
                    case "GunNowCycleCounter":
                        if (Convert.ToInt16(v) != GunNowCycleCounter && Convert.ToInt16(v) != 0 )
                        {
                            GunNowCycleCounter = Convert.ToInt16(v);

                            GunMachAlermType = ""; 
                            CachePlaceRivetOutOverTimeResetStr = "";
                            GunHeadRivetInOverTimeResetStr = "";
                            FeedingNOKAlarmStr = "";
                            InhaleRivetLater_DisableVacuoResetStr = "";



                            GunNowCycleCounter_Start++;
                            GunNowCycleCounter_UP_cnt = 0;
                            GunCachePlaceStartTime = System.DateTime.Now;
                            dt.Rows[GunNowCycleCounter_Start - 1]["铆枪当前循环序号"] = CommonMethods.CurrentValue["_03_HMI.GunNowCycleCounter"];   
                            dt.Rows[GunNowCycleCounter_Start - 1]["铆钉从缓存处到排钉开始日期"] = GunCachePlaceStartTime.ToString("yyyy-MM-dd_HH:mm:ss");
                
                        }
                        break;

                    case "RivetInCounter": RivetInCounter = Convert.ToInt32(v); break;
                    case "RivetOutCounter": RivetOutCounter = Convert.ToInt32(v); break;
                    case "InhaleRivetOKCounter": InhaleRivetOKCounter = Convert.ToInt32(v); break;
                    case "InhaleRivetNOKCounter": InhaleRivetNOKCounter = Convert.ToInt32(v); break;
                    case "InhaleRivetActualCounter":

                        if(Convert.ToInt32(v) == 0 )
                        {
                            EnableVacuoValve_VacuoRchTimeStr = "";
                            SingleCycleVacuoValueStr = "";
                            EnableVacuoValveLater_VacuoValue1Str = "";
                            EnableVacuoValveLater_VacuoValue2Str = "";
                        }

                        if (InhaleRivetActualCounter != Convert.ToInt32(v) )
                        {
                            //FeedMachAlermType
                            InhaleRivetActualCounter = Convert.ToInt32(v);
                            if (Convert.ToInt32(v) != 0)
                            {
                                EnableVacuoValve_VacuoRchTimeStr = EnableVacuoValve_VacuoRchTimeStr + "第" + InhaleRivetActualCounter.ToString() + "次:" + CommonMethods.CurrentValue["_03_HMI.EnableVacuoValve_VacuoRchTime"] + "ms \r\n";

                                SingleCycleVacuoValueStr = SingleCycleVacuoValueStr + "第" + InhaleRivetActualCounter.ToString() + "次:" + CommonMethods.CurrentValue["_03_HMI.VacuoRchLater_SampleTime"]  + "ms " + CommonMethods.CurrentValue["_03_HMI.SingleCycleVacuoValue"] + "bar" + " \r\n";//.ToString("f3")

                                EnableVacuoValveLater_VacuoValue1Str = EnableVacuoValveLater_VacuoValue1Str + "第" + InhaleRivetActualCounter.ToString() + "次:" + CommonMethods.CurrentValue["_03_HMI.EnableVacuoValveLater_SampleTime1"]  + "ms " + CommonMethods.CurrentValue["_03_HMI.EnableVacuoValveLater_VacuoValue1"] + "bar \r\n";//.ToString("f3")

                                EnableVacuoValveLater_VacuoValue2Str = EnableVacuoValveLater_VacuoValue2Str + "第" + InhaleRivetActualCounter.ToString() + "次:" + CommonMethods.CurrentValue["_03_HMI.EnableVacuoValveLater_SampleTime2"] + "ms " + CommonMethods.CurrentValue["_03_HMI.EnableVacuoValveLater_VacuoValue2"] + "bar \r\n"; //.ToString("f3")
                            }

                        }
                         
                        break;
                    case "TestFeedingTime": TestFeedingTime = Convert.ToInt32(v); break;
                    case "EnableVacuoValve_VacuoRchTime": EnableVacuoValve_VacuoRchTime = v; break;

                    case "VacuoRchLater_SampleTime": VacuoRchLater_SampleTime = v; break;
                    case "SingleCycleVacuoValue": SingleCycleVacuoValue = v ; break;
                    case "EnableVacuoValveLater_SampleTime1": EnableVacuoValveLater_SampleTime1 = v; break;
                    case "EnableVacuoValveLater_VacuoValue1": EnableVacuoValveLater_VacuoValue1 = v; break;
                    case "EnableVacuoValveLater_SampleTime2": EnableVacuoValveLater_SampleTime2 = v; break;
                    case "EnableVacuoValveLater_VacuoValue2": EnableVacuoValveLater_VacuoValue2 = v; break;
                         
                    case "GunMachAlermType":
                        if (CachePlaceRivetOutOverTimeResetStr != "")
                        {
                            GunMachAlermType = "缓存处出钉是否超时报警" + " ";
                        }
                        if (GunHeadRivetInOverTimeResetStr != "")
                        {
                            GunMachAlermType += "枪头处进钉是否超时报警" + " ";
                        }
                        if (FeedingNOKAlarmStr != "")
                        {
                            GunMachAlermType += "当前序号铆钉吸钉是否失败报警" + " ";
                        }
                        if (InhaleRivetLater_DisableVacuoResetStr != "")
                        {
                            GunMachAlermType += "吸钉成功后移动是否掉真空报警" + " ";
                        }
                        break;

                    case "FeedMachNowCycleing":
                        try
                        { 
                            if (CommonMethods.CurrentValue["_03_HMI.FeedMachNowCycleing"] != FeedMachNowCycleing_bak && CommonMethods.CurrentValue["_03_HMI.FeedMachNowCycleing"] != "0" )
                            {
                                if(CommonMethods.CurrentValue["_03_HMI.FeedMachNowCycleing"] == "False")
                                {
                                    MachCachePlaceEndTime = System.DateTime.Now;
                                    MachCachePlaceDealTime = DateDiff(MachCachePlaceStartTime, MachCachePlaceEndTime);

                                    dt.Rows[FeedMachNowCycleCounter_Start - 1]["铆钉从振动盘到缓存处结束日期"] = MachCachePlaceEndTime.ToString("yyyy-MM-dd_HH:mm:ss");
                                    dt.Rows[FeedMachNowCycleCounter_Start - 1]["铆钉从振动盘到缓存处用时"] = MachCachePlaceDealTime;

                                    if (FeedMachRivetOutOverTimeResetStr != "" || CachePlaceRivetInOverTimeResetStr != "" || CachePlaceRivetAtOverTimeResetStr != "")
                                        FeedMachOK = "失败"; 
                                    else
                                        FeedMachOK = "成功";

                                    try
                                    {
                                        dt.Rows[FeedMachNowCycleCounter_Start - 1]["送钉机送钉是否成功"] = FeedMachOK;
                                    }
                                    catch 
                                    { 
                                    }
                                } 
                                FeedMachNowCycleing_bak = CommonMethods.CurrentValue["_03_HMI.FeedMachNowCycleing"]; 
                            }
                        }
                        catch 
                        { 
                        } 
                        break;

                    case "GunNowCycleing": 
                       
                        try
                        {
                            if (CommonMethods.CurrentValue["_03_HMI.GunNowCycleing"] != GunNowCycleing_bak && CommonMethods.CurrentValue["_03_HMI.GunNowCycleing"] != "0")
                            {
                                if (CommonMethods.CurrentValue["_03_HMI.GunNowCycleing"] == "False")
                                {
                                    GunCachePlaceEndTime = System.DateTime.Now;
                          
                                    try
                                    {
                                        dt.Rows[GunNowCycleCounter_Start - 1]["铆钉从缓存处到排钉结束日期"] = GunCachePlaceEndTime.ToString("yyyy-MM-dd_HH:mm:ss");
                                        try
                                        {
                                            GunCachePlaceDealTime = DateDiff(GunCachePlaceStartTime,  GunCachePlaceEndTime
                                                        //DateTime.ParseExact(dt.Rows[GunNowCycleCounter_Start - 1]["铆钉从缓存处到排钉开始日期"].ToString().Replace("_", " "), "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture),
                                                        //DateTime.ParseExact(dt.Rows[GunNowCycleCounter_Start - 1]["铆钉从缓存处到排钉结束日期"].ToString().Replace("_", " "), "yyyy-MM-dd HH:mm:ss", System.Globalization.CultureInfo.CurrentCulture)
                                            ); 
                                        }
                                        catch
                                        {
                                        }
                                        dt.Rows[GunNowCycleCounter_Start - 1]["铆钉从缓存处到排钉用时"] = GunCachePlaceDealTime;
                                        if (CachePlaceRivetOutOverTimeResetStr != "" || GunHeadRivetInOverTimeResetStr != "" || FeedingNOKAlarmStr != "" || InhaleRivetLater_DisableVacuoResetStr != "")
                                        {
                                            GunMachOK = "失败"; 
                                        }
                                        else
                                            GunMachOK = "成功";
                                        dt.Rows[GunNowCycleCounter_Start - 1]["铆枪送吸钉是否成功"] = GunMachOK;
                                    }
                                    catch { }
                                }
                                GunNowCycleing_bak = CommonMethods.CurrentValue["_03_HMI.GunNowCycleing"];
                            }
                        }
                        catch
                        {
                        }
                        break;

                    case "FeedMachRivetOutOverTimeAlarm":
                        if (v != FeedMachRivetOutOverTimeAlarm)
                        {
                            if (v == "1")
                            {
                                FeedMachRivetOutOverTimeResetCnt++;
                                FeedMachRivetOutOverTimeResetStr = FeedMachRivetOutOverTimeResetStr + " " + FeedMachRivetOutOverTimeResetCnt.ToString() + ":" + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                                textBox75.Text = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                            }
                            FeedMachRivetOutOverTimeAlarm = v;
                        }
                        break;

                    case "CachePlaceRivetInOverTimeAlarm":
                        if (v != CachePlaceRivetInOverTimeAlarm)
                        {
                            if (v == "1")
                            {
                                CachePlaceRivetInOverTimeResetCnt++;
                                CachePlaceRivetInOverTimeResetStr = CachePlaceRivetInOverTimeResetStr + " " + CachePlaceRivetInOverTimeResetCnt.ToString() + ":" + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                                textBox74.Text = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                                
                            }
                            CachePlaceRivetInOverTimeAlarm = v;
                        }
                        break;

                    case "CachePlaceRivetAtOverTimeAlarm":
                        if (v != CachePlaceRivetAtOverTimeAlarm)
                        {
                            if (v == "1")
                            {
                                CachePlaceRivetAtOverTimeResetCnt++;
                                CachePlaceRivetAtOverTimeResetStr = CachePlaceRivetAtOverTimeResetStr + " " + CachePlaceRivetAtOverTimeResetCnt.ToString() + ":" + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                                textBox73.Text = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                                textBox71.Text = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                            }
                            CachePlaceRivetAtOverTimeAlarm = v;
                        }
                        break;

                    case "FeedMachBlowRivetOverTimeAlarm":
                        if (v != FeedMachBlowRivetOverTimeAlarm)
                        {
                            if (v == "1")
                            {
                                textBox72.Text = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                            }
                            FeedMachBlowRivetOverTimeAlarm = v;
                        }
                        break;

                    case "CachePlaceRivetOutOverTimeAlarm":
                        if (v != CachePlaceRivetOutOverTimeAlarm)
                        {
                            if (v == "1")
                            {
                                CachePlaceRivetOutOverTimeResetCnt++;
                                CachePlaceRivetOutOverTimeResetStr = CachePlaceRivetOutOverTimeResetStr + " " + CachePlaceRivetOutOverTimeResetCnt.ToString() + ":" + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                                textBox70.Text = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); 
                            }
                            CachePlaceRivetOutOverTimeAlarm = v;
                        }
                        break;

                    case "GunHeadRivetInOverTimeAlarm":
                        if (v != GunHeadRivetInOverTimeAlarm)
                        {
                            if (v == "1")
                            {
                                GunHeadRivetInOverTimeResetCnt++;
                                GunHeadRivetInOverTimeResetStr = GunHeadRivetInOverTimeResetStr + " " + GunHeadRivetInOverTimeResetCnt.ToString() + ":" + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                                textBox69.Text = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                            }
                            GunHeadRivetInOverTimeAlarm = v;
                        }
                        break;

                    case "CachePlaceBlowRivetOverTimeAlarm":
                        if (v != CachePlaceBlowRivetOverTimeAlarm)
                        {
                            if (v == "1")
                            {
                                CachePlaceBlowRivetOverTimeResetCnt++;
                                CachePlaceBlowRivetOverTimeResetStr = CachePlaceBlowRivetOverTimeResetStr + " " + CachePlaceBlowRivetOverTimeResetCnt.ToString() + ":" + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                                textBox68.Text = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                            }
                            CachePlaceBlowRivetOverTimeAlarm = v;
                        }
                        break;
 
                    case "FeedingNOK":
                        if (v != FeedingNOK)
                        {
                            if (v == "1")
                            {
                                FeedingNOKAlarmCnt++;
                                FeedingNOKAlarmStr = FeedingNOKAlarmStr + " " + FeedingNOKAlarmCnt.ToString() + ":" + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                                textBox67.Text = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                            }
                            FeedingNOK = v;
                        }
                        break;


                    case "InhaleRivetLater_DisableVacuoAlarm":
                        if (v != InhaleRivetLater_DisableVacuoAlarm)
                        {
                            if (v == "1")
                            {
                                InhaleRivetLater_DisableVacuoAlarmCnt++;
                                InhaleRivetLater_DisableVacuoResetStr = InhaleRivetLater_DisableVacuoResetStr + " " + InhaleRivetLater_DisableVacuoAlarmCnt.ToString() + ":" + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                                textBox66.Text = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                            }
                            InhaleRivetLater_DisableVacuoAlarm = v;
                        }
                        break;

                    default:
                        break;
                }
            }
            catch
            { 
            }
        }
         
        public static string DateDiff(DateTime DateTime1, DateTime DateTime2)
        {
            string dateDiff = "";
            try
            {
                TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
                TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
                TimeSpan ts = ts1.Subtract(ts2).Duration();
                string hours = ts.Hours.ToString(), minutes = ts.Minutes.ToString(), seconds = ts.Seconds.ToString();
                if (ts.Hours < 10)
                {
                    hours = "0" + ts.Hours.ToString();
                }
                if (ts.Minutes < 10)
                {
                    minutes = "0" + ts.Minutes.ToString();
                }
                if (ts.Seconds < 10)
                {
                    seconds = "0" + ts.Seconds.ToString();
                } 
                dateDiff = ts.TotalSeconds.ToString(".#");//hours + ":" + minutes + ":" +seconds +
            }
            catch { dateDiff = ""; }
            return dateDiff;
        }
 
        public void InitDT()
        {
            dt = new DataTable("送钉测试");

            dt.Columns.Add("送吸钉测试编号");
            dt.Columns.Add("送钉机上次循环序号");
            dt.Columns.Add("送钉机当前循环序号");
            dt.Columns.Add("送钉机出钉序号");
            dt.Columns.Add("缓存处进钉序号");
            dt.Columns.Add("送钉机出钉是否超时报警");
            dt.Columns.Add("缓存处进钉是否超时报警");
            dt.Columns.Add("缓存处铆钉到位是否超时报警");
            dt.Columns.Add("送钉机送钉是否成功");
            dt.Columns.Add("送钉机送钉失败报警类别");
            dt.Columns.Add("铆钉从振动盘到缓存处开始日期");
            dt.Columns.Add("铆钉从振动盘到缓存处结束日期");
            dt.Columns.Add("铆钉从振动盘到缓存处用时");
            dt.Columns.Add("      ");
            dt.Columns.Add("铆枪上次循环序号");
            dt.Columns.Add("铆枪当前循环序号");
            dt.Columns.Add("缓存处出钉序号");
            dt.Columns.Add("枪头处进钉序号");
            dt.Columns.Add("吸钉成功计数");
            dt.Columns.Add("吸钉失败序号");
            dt.Columns.Add("当前序号铆钉实际吸钉次数");
            dt.Columns.Add("当前序号铆钉送吸钉成功用时");
            dt.Columns.Add("吸钉阀打开后到真空达到用时");
            dt.Columns.Add("延时后真空值");
            dt.Columns.Add("吸钉阀打开后真空值1");
            dt.Columns.Add("吸钉阀打开后真空值2");
            dt.Columns.Add("缓存处出钉是否超时报警");
            dt.Columns.Add("枪头处进钉是否超时报警");
            dt.Columns.Add("当前序号铆钉吸钉是否失败报警");
            dt.Columns.Add("吸钉成功后移动是否掉真空报警");
            dt.Columns.Add("铆枪送吸钉是否成功");
            dt.Columns.Add("铆枪送吸钉失败报警类别");
            dt.Columns.Add("铆钉从缓存处到排钉开始日期");
            dt.Columns.Add("铆钉从缓存处到排钉结束日期");
            dt.Columns.Add("铆钉从缓存处到排钉用时");

            ID = 0;
            dt.Clear();
            for (int i = 0; i < 20; i++)
            {
                InsertNewDT();
            }
   
        }
 
        public void InsertNewDT()
        { 
            Init();
            ID++;
            dr = dt.NewRow();
            dr["送吸钉测试编号"] = ID;
            dr["送钉机上次循环序号"] = "0";
            dr["送钉机当前循环序号"] = "0";
            dr["送钉机出钉序号"] = "0";
            dr["缓存处进钉序号"] = "0";

            dr["送钉机出钉是否超时报警"] = "0"; 
            dr["缓存处进钉是否超时报警"] = "0";
            dr["缓存处铆钉到位是否超时报警"] = "0";

            dr["送钉机送钉是否成功"] = "0";
            dr["送钉机送钉失败报警类别"] = "0";
            dr["铆钉从振动盘到缓存处开始日期"] = ""; 
            dr["铆钉从振动盘到缓存处结束日期"] = ""; 
            dr["铆钉从振动盘到缓存处用时"] = "0";
            dr["铆枪上次循环序号"] = "0";
            dr["铆枪当前循环序号"] = "0";
            dr["缓存处出钉序号"] = "0";
            dr["枪头处进钉序号"] = "0";
            dr["吸钉成功计数"] = "0";
            dr["吸钉失败序号"] = "0";
            dr["当前序号铆钉实际吸钉次数"] = "0";
            dr["当前序号铆钉送吸钉成功用时"] = "0";


            dr["吸钉阀打开后到真空达到用时"] = "0"; 
            dr["延时后真空值"] = "0"; 
            dr["吸钉阀打开后真空值1"] = "0"; 
            dr["吸钉阀打开后真空值2"] = "0"; 
            dr["缓存处出钉是否超时报警"] = "0"; 
            dr["枪头处进钉是否超时报警"] = "0"; 
            dr["当前序号铆钉吸钉是否失败报警"] = "0"; 
            dr["吸钉成功后移动是否掉真空报警"] = "0"; 
            dr["铆枪送吸钉是否成功"] = "0"; 
            dr["铆枪送吸钉失败报警类别"] = ""; 
            dr["铆钉从缓存处到排钉开始日期"] = ""; 
            dr["铆钉从缓存处到排钉结束日期"] = ""; 
            dr["铆钉从缓存处到排钉用时"] = "0"; 

            dt.Rows.Add(dr);
          
        }

        public void UpdateDT()
        {
            if (dt.Rows.Count > 0)
            { 
                try
                { 
                    if (FeedMachNowCycleCounter_Start >= 1)
                    {  
                        dt.Rows[FeedMachNowCycleCounter_Start - 1]["送钉机出钉序号"] = FeedMachRivetOutCounter;
                        dt.Rows[FeedMachNowCycleCounter_Start - 1]["缓存处进钉序号"] = CachePlaceRivetInCounter;
                        dt.Rows[FeedMachNowCycleCounter_Start - 1]["送钉机出钉是否超时报警"] = FeedMachRivetOutOverTimeResetStr;
                        dt.Rows[FeedMachNowCycleCounter_Start - 1]["缓存处进钉是否超时报警"] = CachePlaceRivetInOverTimeResetStr;
                        dt.Rows[FeedMachNowCycleCounter_Start - 1]["缓存处铆钉到位是否超时报警"] = CachePlaceRivetAtOverTimeResetStr; 
                        dt.Rows[FeedMachNowCycleCounter_Start - 1]["送钉机送钉失败报警类别"] = FeedMachAlermType;  
                    }

                    if (GunNowCycleCounter_Start >= 1)
                    { 
                        dt.Rows[GunNowCycleCounter_Start - 1]["缓存处出钉序号"] = RivetInCounter;
                        dt.Rows[GunNowCycleCounter_Start - 1]["枪头处进钉序号"] = RivetOutCounter;
                        dt.Rows[GunNowCycleCounter_Start - 1]["吸钉成功计数"] = InhaleRivetOKCounter;
                        dt.Rows[GunNowCycleCounter_Start - 1]["吸钉失败序号"] = InhaleRivetNOKCounter;
                        dt.Rows[GunNowCycleCounter_Start - 1]["当前序号铆钉实际吸钉次数"] = InhaleRivetActualCounter;
                        dt.Rows[GunNowCycleCounter_Start - 1]["当前序号铆钉送吸钉成功用时"] = TestFeedingTime;
                        dt.Rows[GunNowCycleCounter_Start - 1]["吸钉阀打开后到真空达到用时"] = EnableVacuoValve_VacuoRchTimeStr; 
                        dt.Rows[GunNowCycleCounter_Start - 1]["延时后真空值"] = SingleCycleVacuoValueStr; 
                        dt.Rows[GunNowCycleCounter_Start - 1]["吸钉阀打开后真空值1"] = EnableVacuoValveLater_VacuoValue1Str; 
                        dt.Rows[GunNowCycleCounter_Start - 1]["吸钉阀打开后真空值2"] = EnableVacuoValveLater_VacuoValue2Str; 
                        dt.Rows[GunNowCycleCounter_Start - 1]["缓存处出钉是否超时报警"] = CachePlaceRivetOutOverTimeResetStr;
                        dt.Rows[GunNowCycleCounter_Start - 1]["枪头处进钉是否超时报警"] = GunHeadRivetInOverTimeResetStr;
                        dt.Rows[GunNowCycleCounter_Start - 1]["当前序号铆钉吸钉是否失败报警"] = FeedingNOKAlarmStr;
                        dt.Rows[GunNowCycleCounter_Start - 1]["吸钉成功后移动是否掉真空报警"] = InhaleRivetLater_DisableVacuoResetStr; 
                        dt.Rows[GunNowCycleCounter_Start - 1]["铆枪送吸钉失败报警类别"] = GunMachAlermType; 
                    } 
                    //SetdgviewFunction(dt); 
                }
                catch(Exception ex)
                { 
                }

                 
            }
        }

        
        public void TestHanlder()
        {
         
            TimeSpan ts = new TimeSpan();



            if (FeedMachRivetOutOverTimeResetStr != "" || CachePlaceRivetInOverTimeResetStr != "" || CachePlaceRivetAtOverTimeResetStr != "")
            {

           
                FeedMachOK = "失败";
                try
                {
                    if (FeedMachNowCycleCounter_Start > 0)
                        dt.Rows[FeedMachNowCycleCounter_Start - 1]["送钉机送钉是否成功"] = FeedMachOK;
                }
                catch { }
            }
            else
                FeedMachOK = "成功";

            if (CachePlaceRivetOutOverTimeResetStr != "" || GunHeadRivetInOverTimeResetStr != "" || FeedingNOKAlarmStr != "" || InhaleRivetLater_DisableVacuoResetStr != "") {
                GunMachOK = "失败";
                try { 
                if (GunNowCycleCounter_Start > 0)
                    dt.Rows[GunNowCycleCounter_Start - 1]["铆枪送吸钉是否成功"] = GunMachOK;
                }
                catch { }
            }
            else
                GunMachOK = "成功";

            FeedAlarmStr = CachePlaceRivetOutOverTimeResetStr + GunHeadRivetInOverTimeResetStr + FeedingNOKAlarmStr + InhaleRivetLater_DisableVacuoResetStr;

            if (FeedAlarmStr != FeedAlarmStr_bak)
            {
                if (CachePlaceRivetOutOverTimeResetStr != "")
                {
                    GunMachAlermType = "缓存处出钉超时报警" + "\r\n ";
                }
                if (GunHeadRivetInOverTimeResetStr != "")
                {
                    GunMachAlermType += "枪头处进钉超时报警" + "\r\n ";
                }
                if (FeedingNOKAlarmStr != "")
                {
                    GunMachAlermType += "当前序号铆钉吸钉失败报警" + "\r\n ";
                }
                if (InhaleRivetLater_DisableVacuoResetStr != "")
                {
                    GunMachAlermType += "吸钉成功后移动掉真空报警" + "\r\n ";
                }
                FeedAlarmStr_bak = FeedAlarmStr;
            }

            GunMachAlermstr = FeedMachRivetOutOverTimeResetStr + CachePlaceRivetInOverTimeResetStr + CachePlaceRivetAtOverTimeResetStr;
            if (GunMachAlermstr != GunMachAlermstr_bak)
            {
                if (FeedMachRivetOutOverTimeResetStr != "")
                {
                    FeedMachAlermType = "送钉机出钉超时报警" + "\r\n ";
                }
                if (CachePlaceRivetInOverTimeResetStr != "")
                {
                    FeedMachAlermType += "缓存处进钉超时报警" + "\r\n ";
                }
                if (CachePlaceRivetAtOverTimeResetStr != "")
                {
                    FeedMachAlermType += "缓存处铆钉到位超时报警" + "\r\n ";
                }
                GunMachAlermstr_bak = GunMachAlermstr;
            }
            
  


            if (CommonMethods.CurrentValue["_03_HMI.FeedMachNowCycleing"] == "True")
            {
                FeedMachLastCycleCounterCnt++;
                if (FeedMachLastCycleCounterCnt > 10) FeedMachLastCycleCounterCnt = 10;
            }
            else
            {
                FeedMachLastCycleCounterCnt = 0; 
            }

 

            if (CommonMethods.CurrentValue["_03_HMI.GunNowCycleing"] == "True")
            {
                GunLastCycleCounterCnt++;
                if (GunLastCycleCounterCnt > 10) GunLastCycleCounterCnt = 10;
            }
            else
            {
                GunLastCycleCounterCnt = 0;
            }


 
        }


        public void AlarmHanlder()
        {
            AlarmStr = FeedMachRivetOutOverTimeAlarm + CachePlaceRivetInOverTimeAlarm + CachePlaceRivetAtOverTimeAlarm + FeedMachBlowRivetOverTimeAlarm + CachePlaceRivetOutOverTimeAlarm + GunHeadRivetInOverTimeAlarm + CachePlaceBlowRivetOverTimeAlarm + FeedingNOK + InhaleRivetLater_DisableVacuoAlarm;

            if (AlarmStr == "000000000")
            {
                label89.Text = "";
                circleLabel14.BackColor = Color.LimeGreen;
            }
            else 
            {
                label89.Text = "警报"; 
                if (AlarmStr != AlarmSpeakStrBak  )
                {
                    circleLabel14.BackColor = Color.Red;
                    if (AlarmCnt == 200) {

                        try
                        {
                            speak.Dispose();
                            speak = new SpeechSynthesizer();
                            speak.Volume = 100;
                            speak.Rate = -1;
                            speak.SpeakAsync("警报");
                        }
                        catch
                        {

                        }
                        AlarmCnt = 0;
                    } 
                    AlarmCnt++;
                } 
            } 
        }

        Stopwatch sw = new Stopwatch();
        private void timer1_Tick(object sender, EventArgs e)
        {
            switch (Beckhoff_PLC.MachineType)
            {
                case "":
                case "0":
                case "1":
                    checkBox1.Text = "原型送钉机";
                    button6.Visible = true;
                    label127.Visible = true;
                    label102.Visible = true;
                    label113.Visible = true;
                    label114.Visible = true;
                    break;
                case "2":
                    checkBox1.Text = "城必达送钉机";
                    button6.Visible = false;
                    label127.Visible = false;
                    label102.Visible = false;
                    label113.Visible = false;
                    label114.Visible = false;
                    break;
                default:
                    break;
            }
            //  sw.Start();
            foreach (TabPage page in this.tabControl1.TabPages)
            {
                foreach (Control ct in page.Controls)
                {

                    if (ct is TextBoxControl && ct.Tag != null  )
                    {
                        if (CommonMethods.CurrentValue != null && CommonMethods.CurrentValue.ContainsKey(ct.Tag.ToString()))
                        {
                            //this.ActualValue = CommonMethods.CurrentValue[this.Tag.ToString()];   //((CheckBox)item)
                            ((TextBoxControl)ct).ActualValue = CommonMethods.CurrentValue[ct.Tag.ToString()];
                        }
                    }



                    if (ct is TextBox && ct.Tag != null && ct.Tag.ToString() != "" )
                    {
 
                        if (CommonMethods.CurrentValue != null && CommonMethods.CurrentValue.ContainsKey(ct.Tag.ToString()))
                        {
                            ct.Text = CommonMethods.CurrentValue[ct.Tag.ToString()].ToString();
                        }
                        GetV(ct.Tag.ToString(), CommonMethods.CurrentValue[ct.Tag.ToString()]);
                    }

                    if (ct is Label && ct.Tag != null && ct.Tag.ToString() != "" )
                    {
                        if (ct.Tag.ToString().ToLower().IndexOf("alarm") != -1 || ct.Tag.ToString().IndexOf("FeedingNOK") != -1)
                        {
                            if (CommonMethods.CurrentValue[ct.Tag.ToString()] == "True" && CommonMethods.CurrentValue.ContainsKey(ct.Tag.ToString()))
                            {
                                ct.BackColor = Color.Red;   //Color.FromArgb(60, 179, 113);
                            }
                            else
                            {
                                ct.BackColor = Color.LimeGreen;//.FromArgb(255, 255, 255);
                            }
                        }
                        else
                        { 
                            if(ct.Name.ToString().ToLower().IndexOf("circle") != -1 )
                            { 
                                if (CommonMethods.CurrentValue[ct.Tag.ToString()] == "True" && CommonMethods.CurrentValue.ContainsKey(ct.Tag.ToString()))
                                {
                                    ct.BackColor = Color.LimeGreen;   //Color.FromArgb(60, 179, 113);
                                }
                                else
                                {
                                    ct.BackColor = Color.Olive;//.FromArgb(255, 255, 255);
                                }  
                            }
                            else
                            {
                                if (CommonMethods.CurrentValue[ct.Tag.ToString()] == "True" && CommonMethods.CurrentValue.ContainsKey(ct.Tag.ToString()) )
                                {
                                    ct.BackColor = Color.FromArgb(60, 179, 113);
                                }
                                else
                                {
                                    ct.BackColor = Color.Gray;// Color.FromArgb(255, 255, 255);
                                } 
                            } 
                        }

                        GetV(ct.Tag.ToString(), CommonMethods.CurrentValue[ct.Tag.ToString()]);
                    }

                    if (ct is Button && ct.Tag != null && ct.Tag.ToString() != "" )
                    {
                        if (CommonMethods.CurrentValue[ct.Tag.ToString()] == "True" && CommonMethods.CurrentValue.ContainsKey(ct.Tag.ToString()))
                        {
                            ct.BackColor = Color.FromArgb(60, 179, 113);
                        }
                        else
                        {
                            ct.BackColor = Color.FromArgb(255, 255, 255);
                        }
                        GetV(ct.Tag.ToString(), CommonMethods.CurrentValue[ct.Tag.ToString()]);
                    }
                }
            }

            foreach (Control ct in this.Controls)
            {
                if (ct is TextBox && ct.Tag != null && ct.Tag.ToString() != "")
                {
                    if (CommonMethods.CurrentValue != null && CommonMethods.CurrentValue.ContainsKey(ct.Tag.ToString()))
                    {
                        ct.Text = CommonMethods.CurrentValue[ct.Tag.ToString()];
                    }
                }

                if (ct is Label && ct.Tag != null && ct.Tag.ToString() != "" )
                {
                    if (CommonMethods.CurrentValue[ct.Tag.ToString()] == "True" && CommonMethods.CurrentValue.ContainsKey(ct.Tag.ToString()))
                    {
                        ct.BackColor = Color.FromArgb(60, 179, 113);
                    }
                    else
                    {
                        ct.BackColor = Color.Gray;// Color.FromArgb(255, 255, 255); Color.FromArgb(255, 255, 255);
                    }
                }

                if (ct is Button && ct.Tag != null && ct.Tag.ToString() != "" )
                {
                    if (CommonMethods.CurrentValue[ct.Tag.ToString()] == "True" && CommonMethods.CurrentValue.ContainsKey(ct.Tag.ToString()))
                    {
                        ct.BackColor = Color.FromArgb(60, 179, 113);
                    }
                    else
                    {
                        ct.BackColor = Color.FromArgb(255, 255, 255);
                    }
                }
            }

            if (CommonMethods.CurrentValue["_03_HMI.FeedMachModel"] == "1")
            {
                checkBox1.Checked = true;
                checkBox2.Checked = false;
            }
            else if (CommonMethods.CurrentValue["_03_HMI.FeedMachModel"] == "2")
            {
                checkBox1.Checked = false;
                checkBox2.Checked = true;
            }

            for (int i = 0; i < this.dgv_data.Columns.Count; i++)
            {
                dgv_data.Columns[i].SortMode = DataGridViewColumnSortMode.NotSortable;
            }

            TestHanlder();
            AlarmHanlder();

            //FeedMachLastCycleCounter
            //textBox10.Text = InhaleRivetActualCounter.ToString();
            // sw.Stop();
            try
            {
                if (btFeedingTestStart_Stop == "1")
                {
                    UpdateDT();
                }
               // dgv_data.DataSource = dt;

            }
            catch
            {

            }
            //MessageBox.Show(  sw.ElapsedMilliseconds.ToString());
        }

        private void button2_MouseDown(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btCachePlaceRivetInCounterClean", true);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button2_MouseUp(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true; 
                Beckhoff_PLC.WriteVariable("_03_HMI.btCachePlaceRivetInCounterClean", false);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }


        private void button3_MouseDown(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btCachePlaceRivetOutCounterClean", true);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button3_MouseUp(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btCachePlaceRivetOutCounterClean", false);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }


        private void button5_MouseDown(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btGunHeadRivetInCounterClean", true);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button5_MouseUp(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btGunHeadRivetInCounterClean", false);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }


        private void button4_MouseDown(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btInhaleRivetOKCounterClean", true);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button4_MouseUp(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btInhaleRivetOKCounterClean", false);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }


        private void button8_MouseDown(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btFeedMachRivetOutCounterClean", true);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button8_MouseUp(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btFeedMachRivetOutCounterClean", false);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }


        private void button6_Click(object sender, EventArgs e)
        {
            Beckhoff_PLC.IsWriting = true;
            if (CommonMethods.CurrentValue["_03_HMI.btFeedMachPushRivetValve"] == "False")
            {
                Beckhoff_PLC.WriteVariable("_03_HMI.btFeedMachPushRivetValve", true);
            }
            if (CommonMethods.CurrentValue["_03_HMI.btFeedMachPushRivetValve"] == "True")
            {
                Beckhoff_PLC.WriteVariable("_03_HMI.btFeedMachPushRivetValve", false);
            }
            Beckhoff_PLC.IsWriting = false;
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Beckhoff_PLC.IsWriting = true;
            if (CommonMethods.CurrentValue["_03_HMI.btFeedMachBlowRivetValve"] == "False")
            {
                Beckhoff_PLC.WriteVariable("_03_HMI.btFeedMachBlowRivetValve", true);
            }
            if (CommonMethods.CurrentValue["_03_HMI.btFeedMachBlowRivetValve"] == "True")
            {
                Beckhoff_PLC.WriteVariable("_03_HMI.btFeedMachBlowRivetValve", false);
            }
            Beckhoff_PLC.IsWriting = false;
        }

        private void btn_Debug_Click(object sender, EventArgs e)
        {
            Beckhoff_PLC.IsWriting = true;
            if (CommonMethods.CurrentValue["_03_HMI.btDebug"] == "False")
            {
                Beckhoff_PLC.WriteVariable("_03_HMI.btDebug", true);
            }
            if (CommonMethods.CurrentValue["_03_HMI.btDebug"] == "True")
            {
                Beckhoff_PLC.WriteVariable("_03_HMI.btDebug", false);
            }
            Beckhoff_PLC.IsWriting = false;
        }

        private void btn_YV1_Click(object sender, EventArgs e)
        {
            Beckhoff_PLC.IsWriting = true;
            if (CommonMethods.CurrentValue["_03_HMI.btEnableVacuoValve"] == "False")
            {
                Beckhoff_PLC.WriteVariable("_03_HMI.btEnableVacuoValve", true);
            }
            if (CommonMethods.CurrentValue["_03_HMI.btEnableVacuoValve"] == "True")
            {
                Beckhoff_PLC.WriteVariable("_03_HMI.btEnableVacuoValve", false);
            }
            Beckhoff_PLC.IsWriting = false;
        }

        private void btn_YV2_Click(object sender, EventArgs e)
        {
            Beckhoff_PLC.IsWriting = true;
            if (CommonMethods.CurrentValue["_03_HMI.btDisableVacuoValve"] == "False")
            {
                Beckhoff_PLC.WriteVariable("_03_HMI.btDisableVacuoValve", true);
            }
            if (CommonMethods.CurrentValue["_03_HMI.btDisableVacuoValve"] == "True")
            {
                Beckhoff_PLC.WriteVariable("_03_HMI.btDisableVacuoValve", false);
            }
            Beckhoff_PLC.IsWriting = false;
        }

        private void btn_YV3_Click(object sender, EventArgs e)
        {
            Beckhoff_PLC.IsWriting = true;
            if (CommonMethods.CurrentValue["_03_HMI.btPrePressureExtendValve"] == "False")
            {
                Beckhoff_PLC.WriteVariable("_03_HMI.btPrePressureExtendValve", true);
            }
            if (CommonMethods.CurrentValue["_03_HMI.btPrePressureExtendValve"] == "True")
            {
                Beckhoff_PLC.WriteVariable("_03_HMI.btPrePressureExtendValve", false);
            }
            Beckhoff_PLC.IsWriting = false;
        }

        private void btn_YV4_Click(object sender, EventArgs e)
        {
            Beckhoff_PLC.IsWriting = true;
            if (CommonMethods.CurrentValue["_03_HMI.btPrePressureRetractValve"] == "False")
            {
                Beckhoff_PLC.WriteVariable("_03_HMI.btPrePressureRetractValve", true);
            }
            if (CommonMethods.CurrentValue["_03_HMI.btPrePressureRetractValve"] == "True")
            {
                Beckhoff_PLC.WriteVariable("_03_HMI.btPrePressureRetractValve", false);
            }
            Beckhoff_PLC.IsWriting = false;
        }

        private void btn_YV5_Click(object sender, EventArgs e)
        {
            Beckhoff_PLC.IsWriting = true;
            if (CommonMethods.CurrentValue["_03_HMI.btCachePlacePushRivetValve"] == "False")
            {
                Beckhoff_PLC.WriteVariable("_03_HMI.btCachePlacePushRivetValve", true);
            }
            if (CommonMethods.CurrentValue["_03_HMI.btCachePlacePushRivetValve"] == "True")
            {
                Beckhoff_PLC.WriteVariable("_03_HMI.btCachePlacePushRivetValve", false);
            }
            Beckhoff_PLC.IsWriting = false;
        }

        private void btn_YV6_Click(object sender, EventArgs e)
        {
            Beckhoff_PLC.IsWriting = true;
            if (CommonMethods.CurrentValue["_03_HMI.btClampValve"] == "False")
            {
                Beckhoff_PLC.WriteVariable("_03_HMI.btClampValve", true);
            }
            if (CommonMethods.CurrentValue["_03_HMI.btClampValve"] == "True")
            {
                Beckhoff_PLC.WriteVariable("_03_HMI.btClampValve", false);
            }
            Beckhoff_PLC.IsWriting = false;
        }

        private void btn_YV7_Click(object sender, EventArgs e)
        {
            Beckhoff_PLC.IsWriting = true;
            if (CommonMethods.CurrentValue["_03_HMI.btCachePlaceBlowRivetValve"] == "False")
            {
                Beckhoff_PLC.WriteVariable("_03_HMI.btCachePlaceBlowRivetValve", true);
            }
            if (CommonMethods.CurrentValue["_03_HMI.btCachePlaceBlowRivetValve"] == "True")
            {
                Beckhoff_PLC.WriteVariable("_03_HMI.btCachePlaceBlowRivetValve", false);
            }
            Beckhoff_PLC.IsWriting = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void button10_MouseDown(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btFeedMachLastCycleCounterClean", true);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button10_MouseUp(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btFeedMachLastCycleCounterClean", false);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button11_MouseDown(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btFeedMachNowCycleCounterClean", true);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button11_MouseUp(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btFeedMachNowCycleCounterClean", false);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button17_MouseDown(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btCachePlaceRivetOutCounterClean", true);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button17_MouseUp(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btCachePlaceRivetOutCounterClean", false);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button18_MouseDown(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btGunHeadRivetInCounterClean", true);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button18_MouseUp(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btGunHeadRivetInCounterClean", false);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button22_Click(object sender, EventArgs e)
        {


            textBox23.Text = CommonMethods.CurrentValue["_03_HMI.FeedMachNowCycleing"];


            AlarmStr = AlarmSpeakStrBak;
            AlarmCnt = 0;
        }

        private void button25_Click(object sender, EventArgs e)
        {
 

        }

        private void button12_Click_1(object sender, EventArgs e)
        {
 
        }

        private void button23_Click(object sender, EventArgs e)
        {
 
        }

        private void button24_Click(object sender, EventArgs e)
        {
 
        }

        private void button26_Click(object sender, EventArgs e)
        { 
 
        }

        private void button27_Click(object sender, EventArgs e)
        { 
 
        }

        private void button32_Click(object sender, EventArgs e)
        {
 
        }

        private void button28_Click(object sender, EventArgs e)
        { 
 
        }

        private void button29_Click(object sender, EventArgs e)
        {
            

   

        }

        private void button30_Click(object sender, EventArgs e)
        {
            
 

        }

        private void button33_Click(object sender, EventArgs e)
        {
            FileName = "送钉测试统计表_" + System.DateTime.Now.ToString("yyyyMMddHHmm") + ".csv";
            UpdateDT();
            Toexcel.DataTableToCSV(dt, System.Environment.CurrentDirectory + "\\Log\\" + FileName);
        }

        private void button19_MouseDown(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btInhaleRivetOKCounterClean", true);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button19_MouseUp(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btInhaleRivetOKCounterClean", false);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button20_MouseDown(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btInhaleRivetNOKCounterClean", true);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button20_MouseUp(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btInhaleRivetNOKCounterClean", false);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button21_MouseDown(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btInhaleRivetActualCounterClean", true);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button21_MouseUp(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btInhaleRivetActualCounterClean", false);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button12_MouseDown(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.FeedMachRivetOutOverTimeReset", true);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button12_MouseUp(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.FeedMachRivetOutOverTimeReset", false);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button23_MouseDown(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.CachePlaceRivetInOverTimeReset", true);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button23_MouseUp(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.CachePlaceRivetInOverTimeReset", false);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button24_MouseDown(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.CachePlaceRivetAtOverTimeReset", true);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button24_MouseUp(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.CachePlaceRivetAtOverTimeReset", false);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button25_MouseDown(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.FeedMachBlowRivetOverTimeReset", true);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button25_MouseUp(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.FeedMachBlowRivetOverTimeReset", false);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button26_MouseDown(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.CachePlaceRivetAtOverTimeReset", true);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button26_MouseUp(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.CachePlaceRivetAtOverTimeReset", false);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button27_MouseDown(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.CachePlaceRivetOutOverTimeReset", true);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button27_MouseUp(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.CachePlaceRivetOutOverTimeReset", false);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button28_MouseDown(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.GunHeadRivetInOverTimeReset", true);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button28_MouseUp(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.GunHeadRivetInOverTimeReset", false);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button29_MouseDown(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.CachePlaceBlowRivetOverTimeReset", true);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;

        }

        private void button29_MouseUp(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.CachePlaceBlowRivetOverTimeReset", false);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button30_MouseDown(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.FeedingNOKLater_CycleContinue", true);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void tabPage2_Click_1(object sender, EventArgs e)
        {

        }

        private void button30_MouseUp(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.FeedingNOKLater_CycleContinue", false);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button34_Click_1(object sender, EventArgs e)
        {
            dgv_data.DataSource = dt;
        }

        private void textBox24_TextChanged(object sender, EventArgs e)
        {
            if (int.TryParse(textBox24.Text, out int value))
            {
                if (value <= 0)
                {
                    textBox24.Text = 0.ToString();
                }
                else if (value >= 40000)
                {
                    textBox24.Text = 40000.ToString();
                }
            }
        }

        private void button34_Click(object sender, EventArgs e)
        {
            
        }

        private void button31_MouseDown(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.InhaleRivetLater_DisableVacuoReset", true);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button31_MouseUp(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.InhaleRivetLater_DisableVacuoReset", false);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void dgv_data_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {
          // dgv_data.Rows[e.RowIndex].DefaultCellStyle.Font.BackColor = Color.Black;
        }

        private void dgv_data_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {

            e.CellStyle.ForeColor = Color.Black;
            
        }

        private void button31_Click(object sender, EventArgs e)
        {
             
        }

        private void tabPage2_Click(object sender, EventArgs e)
        {

        }

        private void tabPage7_Click(object sender, EventArgs e)
        {

        }

        private void button14_MouseDown(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btGunLastCycleCounterClean", true);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button14_MouseUp(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btGunLastCycleCounterClean", false);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button15_MouseDown(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btGunNowCycleCounterClean", true);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;

        }


        public int testflag = 0;
        public string teststr = "";
        SpeechSynthesizer speak = new SpeechSynthesizer();
        private void button16_Click(object sender, EventArgs e)
        {
    
   

        }

        private void button15_MouseUp(object sender, MouseEventArgs e)
        {
            bool btrig = true;
            if (btrig)
            {
                Beckhoff_PLC.IsWriting = true;
                Beckhoff_PLC.WriteVariable("_03_HMI.btGunNowCycleCounterClean", false);
                Beckhoff_PLC.IsWriting = false;
            }
            btrig = false;
        }

        private void button8_Click(object sender, EventArgs e)
        {

        }

        private void btn_YV8_Click(object sender, EventArgs e)
        {
            Beckhoff_PLC.IsWriting = true;
            if (CommonMethods.CurrentValue["_03_HMI.btGunHeadBlowRivetValve"] == "False")
            {
                Beckhoff_PLC.WriteVariable("_03_HMI.btGunHeadBlowRivetValve", true);
            }
            if (CommonMethods.CurrentValue["_03_HMI.btGunHeadBlowRivetValve"] == "True")
            {
                Beckhoff_PLC.WriteVariable("_03_HMI.btGunHeadBlowRivetValve", false);
            }
            Beckhoff_PLC.IsWriting = false;
        }

        //public void DataHelper()
        //{ 
        //    Init();
        //    InitDT();  
        //    while (true)
        //    { 
        //        Thread.Sleep(50);
        //    }
        ////////////}

        private void FrmSysDebug_Load_1(object sender, EventArgs e)
        {

            Init();
            InitDT();

            //t1 = new Thread(DataHelper);
            //t1.IsBackground = true;
            //t1.Start();

 

        }

        private void label11_Click(object sender, EventArgs e)
        {

        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void label35_Click(object sender, EventArgs e)
        {

        }

        private void tabPage3_Click(object sender, EventArgs e)
        {

        }

        private void button12_Click(object sender, EventArgs e)
        {
 
        }

        private void button10_Click(object sender, EventArgs e)
        {
 
        }

        private void button11_Click(object sender, EventArgs e)
        {
 
        }

        private void button17_Click(object sender, EventArgs e)
        {
 
        }

        private void button18_Click(object sender, EventArgs e)
        {
 
        }

        private void button14_Click(object sender, EventArgs e)
        {
 
        }

        private void button15_Click(object sender, EventArgs e)
        {
 
        }

        private void button19_Click(object sender, EventArgs e)
        {
 
        }

        private void button20_Click(object sender, EventArgs e)
        {
 
        }

        private void button21_Click(object sender, EventArgs e)
        {
 
        }

        private void button13_Click(object sender, EventArgs e)
        {
            Beckhoff_PLC.IsWriting = true;
            if (CommonMethods.CurrentValue["_03_HMI.btDay_Night"] == "False")
            {
                Beckhoff_PLC.WriteVariable("_03_HMI.btDay_Night", true);
                button13.Text = "夜";
            }
            if (CommonMethods.CurrentValue["_03_HMI.btDay_Night"] == "True")
            {
                Beckhoff_PLC.WriteVariable("_03_HMI.btDay_Night", false);
                button13.Text = "昼";
            }
            Beckhoff_PLC.IsWriting = false;
        }

        private void label7_Click(object sender, EventArgs e)
        {

        }
    }


    public class CircleLabel : Label//继承标签类    重新生成解决方案就能看见我啦
    {
        protected override void OnPaint(PaintEventArgs e)//重新设置控件的形状   protected 保护  override重新
        {
            base.OnPaint(e);//递归  每次重新都发生此方法,保证其形状为自定义形状
            System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
            path.AddEllipse(2, 2, this.Width - 6, this.Height - 6);
            //Graphics g = e.Graphics;
            //g.DrawEllipse(new Pen(Color.Red, 2), 2, 2, Width - 6, Height - 6);
            Region = new Region(path);
        }
    }
}
